package com.ruihu.admin.module.business.node.config.controller;

import com.ruihu.admin.common.anno.OperateLog;
import com.ruihu.admin.common.domain.PageResultDTO;
import com.ruihu.admin.common.controller.BaseController;
import com.ruihu.admin.common.domain.ResponseDTO;
import com.ruihu.admin.common.domain.ValidateList;
import com.ruihu.admin.constant.NodeConfigTypeEnum;
import com.ruihu.admin.module.business.node.config.domain.dto.TaskProcessConfigAddDTO;
import com.ruihu.admin.module.business.node.config.domain.dto.TaskProcessConfigUpdateDTO;
import com.ruihu.admin.module.business.node.config.domain.dto.TaskProcessConfigQueryDTO;
import com.ruihu.admin.module.business.node.config.domain.vo.TaskProcessConfigVO;
import com.ruihu.admin.module.business.node.config.domain.vo.TaskProcessConfigExcelVO;
import com.ruihu.admin.module.business.node.config.domain.vo.TaskProcessTypeVO;
import com.ruihu.admin.module.business.node.config.service.TaskProcessConfigService;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.Workbook;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * [ 节点配置表 ]
 *
 * @author Tony
 * @version 1.0
 * @company 瑞虎（南京）信息技术有限公司
 * @copyright (c)  瑞虎（南京）信息技术有限公司Inc. All rights reserved.
 * @date 2022-12-27 16:32:13
 * @since JDK1.8
 */
@RestController
@Api(tags = {"节点配置表"})
@OperateLog
public class TaskProcessConfigController extends BaseController {

    @Autowired
    private TaskProcessConfigService taskProcessConfigService;

    @ApiOperation(value = "分页查询节点配置表",notes = "@author Tony")
    @PostMapping("/taskProcessConfig/page/query")
    public ResponseDTO<PageResultDTO<TaskProcessConfigVO>> queryByPage(@RequestBody TaskProcessConfigQueryDTO queryDTO) {
        return taskProcessConfigService.queryByPage(queryDTO);
    }

    @ApiOperation(value = "获取节点配置类型",notes = "@author Tony")
    @GetMapping("/taskProcessConfig/type/query")
    public ResponseDTO<List<TaskProcessTypeVO>> getConfigType() {
        List<TaskProcessTypeVO> taskProcessTypeVOS = new ArrayList<>();
        for(NodeConfigTypeEnum configTypeEnum: NodeConfigTypeEnum.values()){
            TaskProcessTypeVO taskProcessTypeVO = new TaskProcessTypeVO();
            taskProcessTypeVO.setCode(configTypeEnum.getCode());
            taskProcessTypeVO.setMessage(configTypeEnum.getMessage());
            taskProcessTypeVOS.add(taskProcessTypeVO);
        }
        return ResponseDTO.succData(taskProcessTypeVOS);
    }

    @ApiOperation(value = "添加节点配置表",notes = "@author Tony")
    @PostMapping("/taskProcessConfig/add")
    public ResponseDTO<String> add(@RequestBody @Validated TaskProcessConfigAddDTO addTO){
        return taskProcessConfigService.add(addTO);
    }

    @ApiOperation(value="修改节点配置表",notes = "@author Tony")
    @PostMapping("/taskProcessConfig/update")
    public ResponseDTO<String> update(@RequestBody @Validated TaskProcessConfigUpdateDTO updateDTO){
        return taskProcessConfigService.update(updateDTO);
    }

    @ApiOperation(value="批量删除节点配置表",notes = "@author Tony")
    @PostMapping("/taskProcessConfig/deleteByIds")
    public ResponseDTO<String> delete(@RequestBody @Validated ValidateList<Long> idList) {
        return taskProcessConfigService.deleteByIds(idList);
    }

    @ApiOperation(value="删除节点配置表",notes = "@author Tony")
    @GetMapping("/taskProcessConfig/deleteById/{taskNodeConfigId}")
    public ResponseDTO<String> deleteById(@PathVariable Long taskNodeConfigId) {
        return taskProcessConfigService.deleteById(taskNodeConfigId);
    }

    @ApiOperation(value = "批量导出", notes = "@author Tony")
    @PostMapping("/taskProcessConfig/export/batch")
    public void batchExport(@RequestBody @Validated ValidateList<Long> idList, HttpServletResponse response) {
        //查询数据
        List<TaskProcessConfigExcelVO> taskProcessConfigList = taskProcessConfigService.queryBatchExportData(idList);
        //导出操作
        ExportParams ex = new ExportParams("节点配置表", "Sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(ex, TaskProcessConfigExcelVO.class, taskProcessConfigList);
        downloadExcel("节点配置表", workbook, response);
    }

    @ApiOperation(value = "导出全部", notes = "@author Tony")
    @PostMapping("/taskProcessConfig/export/all")
    public void exportAll(@RequestBody @Validated TaskProcessConfigQueryDTO queryDTO, HttpServletResponse response) {
        //查询数据
        List<TaskProcessConfigExcelVO> taskProcessConfigList = taskProcessConfigService.queryAllExportData(queryDTO);
        //导出操作
        ExportParams ex = new ExportParams("节点配置表", "Sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(ex, TaskProcessConfigExcelVO.class, taskProcessConfigList);
        downloadExcel("节点配置表", workbook, response);
    }

}
